home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 363 / tprolog1 / alpha next >
Text File  |  1990-02-03  |  2KB  |  58 lines

  1. go :- cls, display('***********'), nl, nl, tag(main), fail.
  2. go :- nl, display('*********** - ended.'), nl.
  3.  
  4. main :- repeat,
  5.       getwords(Words), compile(Words, Cmd), do(Cmd), fail.
  6.  
  7. % READER and SCANNER
  8.  
  9. getwords(Words) :- rdchsk(FirstCh), getwords(FirstCh, Words).
  10.  
  11. getwords(Eoln, []) :- iseoln(Eoln), !.
  12. getwords(Noletter, Words) :- not letter(Noletter), !, rch,
  13.       lastch(NextCh), getwords(NextCh, Words).
  14. getwords(Letter, [Word | Words]) :- absorb(Letter, String, NextCh),
  15.       pname(Word, String), getwords(NextCh, Words).
  16.  
  17. absorb(Noletter, [], Noletter) :- not letter(Noletter), !.
  18. absorb(Letter, [Small | Letters], NextCh) :- small(Letter, Small),
  19.       rch, lastch(MiddleCh), absorb(MiddleCh, Letters, NextCh).
  20.  
  21. small(Small, Small) :- smalletter(Small), !.
  22. small(Big, Small) :-
  23.       ordchr(BOrd, Big), sum(BOrd, 32, SOrd), ordchr(SOrd, Small).
  24.  
  25. % parse a command and execute it
  26.  
  27. compile(WList, Cmd) :- phrase(command(Cmd), WList), !.
  28. compile(WList, _) :- display('Can''t compile " '), showlist(WList),
  29.       display('".'), nl, fail.
  30.  
  31. showlist([]) :- !.
  32. showlist([Word | Words]) :- display(Word), wch(' '), showlist(Words).
  33.  
  34.  
  35. do(Cmd) :- Cmd, !, display('OK.'), nl.
  36. do(_) :- display('I can''t !'), nl.
  37.  
  38. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  39.  
  40. command( assert(T) ) -->
  41.    [Name1, 'is'], article, [Name2], !, { T =.. [Name2, Name1] }.
  42.  
  43. command( assert( (Left :- Right) ) ) -->
  44.    article, [Name1, 'is'], article, [Name2], !,
  45.    { Left =.. [Name2, X], Right =.. [Name1, X] }.
  46.  
  47. command( tagfail(main) ) --> [bye], !.
  48.  
  49. command( (Term, display('Yes.'), nl) ; (display('I don''t know !'), nl) ) -->
  50.    ['is', Name1], article, [Name2], !,
  51.    { Term =.. [Name2, Name1] }.
  52.  
  53. article --> [a], !.
  54. article --> [an].
  55.  
  56. end.
  57.  
  58.